﻿using System;
using System.Collections.Generic;
using System.Data;
using Ue.Framework.Collections;
using Ue.Infrastructure;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.TreeViews;

namespace Ue.Qcdz.Repositories.TreeViews
{
    public class TreeViewRepository : SqlServerRepository<TreeView>,ITreeViewRepository
    {
        private IRepository<TreeNode> nodeRepository;

        public TreeViewRepository()
            : this(null) { }

        public TreeViewRepository(IUnitOfWork uniteOfWork)
            :base(uniteOfWork)
        {
            this.nodeRepository = new TreeNodeRepository();
        }

        #region SqlServerRepository

        protected override string GetDataTableName()
        {
            return TreeViewFactory.TableName;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[]{
                TreeViewFactory.FieldNames.EntityKey,
                TreeViewFactory.FieldNames.Name,
                TreeViewFactory.FieldNames.Description,
                TreeViewFactory.FieldNames.CreateOn
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]{
                TreeViewFactory.FieldNames.Name,
                TreeViewFactory.FieldNames.Description
            };
        }

        protected override IDataParameter[] BuildParameters(TreeView entity)
        {
            return new IDataParameter[]{
                this.RepositoryDriver.BuildParameter(TreeViewFactory.FieldNames.EntityKey,entity.Key),
                this.RepositoryDriver.BuildParameter(TreeViewFactory.FieldNames.Name,entity.Name),
                this.RepositoryDriver.BuildParameter(TreeViewFactory.FieldNames.Description,entity.Description),
                this.RepositoryDriver.BuildParameter(TreeViewFactory.FieldNames.CreateOn,entity.CreateOn)
            };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            return null;
        }

        protected override IDictionary<string, SqlServerRepositoryBase<TreeView>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        } 

        #endregion

        #region override SqlServerRepository

        protected override void PersistNewItem(TreeView item)
        {
            base.PersistNewItem(item);

            item.ChildNodes.Foreach(node =>
            {
                this.nodeRepository[node.Key] = node;
            });

            //需要从数据库构造一下，生成TreeViewProxy对象。而不再是TreeView对象
            //此功未完成，未找到完美解决方法。
            //item = this.FindBy(item.Key);
        }

        protected override void PersistDeletedItem(TreeView item)
        {
            item.ChildNodes.Foreach(node =>
            {
                this.nodeRepository.Remove(node);
            });

            base.PersistDeletedItem(item);
        }

        protected override void PersistUpdatedItem(TreeView item)
        {
            base.PersistUpdatedItem(item);

            TreeViewProxy treeViewProxy=item as TreeViewProxy;
            if (treeViewProxy!=null)
            {
                treeViewProxy.UniteOfWork.Commit();
            }
        }

        #endregion
    }
}
